package com.jack.topvision.tvcamera2;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.topvision.jack.camerasdk2.camera.utils.FileUtils;
import com.topvision.jack.camerasdk2.admin.TvCameraSdk;
import com.topvision.jack.camerasdk2.camera.utils.Mp4ParseUtil;
import com.topvision.jack.camerasdk2.camera.view.CameraSurfaceView;
import com.topvision.jack.camerasdk2.camera.callback.OnCameraListener;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 拍照/摄像
 * 创建时间:2017/12/27
 * 作者 jack
 * 描述
*/
public class CameraActivity extends Activity implements OnCameraListener, View.OnClickListener {
    public final String TAG = CameraActivity.class.getSimpleName();
    public final int CAMERA_ID = Camera.CameraInfo.CAMERA_FACING_BACK;
    CameraSurfaceView cameraSurfaceView;
    Camera camera;
    String outputPath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initCameraSurfaceView();
        initView();
    }

    private void initView() {
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        findViewById(R.id.resume).setOnClickListener(this);
        findViewById(R.id.testMerge).setOnClickListener(this);
        findViewById(R.id.takePhoto).setOnClickListener(this);
        findViewById(R.id.delete).setOnClickListener(this);
    }

    private void initCameraSurfaceView() {
        cameraSurfaceView = (CameraSurfaceView) findViewById(R.id.camera_surface_view);
        cameraSurfaceView.setOnCameraListener(this);
        //设置为竖屏模式,默认为横屏模式
//        cameraSurfaceView.setPortraitMode();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cameraSurfaceView.onPause();
    }

    long startTime;

    @Override
    public Camera setupCamera() {
        //camera是TvCameraSDK的句柄对象
        startTime = System.currentTimeMillis();
        camera = TvCameraSdk.getInstance().openCamera(getApplicationContext(), CAMERA_ID);
        //SDK 提供了最优预览分辨率方法
//        TvCameraSdk.getInstance().setBestPreviewSize(getApplicationContext());
        TvCameraSdk.getInstance().setPreviewSize(getApplicationContext(), 1920, 1080);
        TvCameraSdk.getInstance().setPictureSize(getApplicationContext(), 1920, 1080);
        return camera;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start:
                //开启录制
                String folder = Environment.getExternalStorageDirectory().getPath() + File.separator + "cameraTest";
                FileUtils.createFolder(getApplication(), folder);
                outputPath = folder + File.separator + System.currentTimeMillis() + ".mp4";
                int resolution = CamcorderProfile.QUALITY_1080P;
                TvCameraSdk.getInstance().startRecord(getApplicationContext(), cameraSurfaceView, outputPath, resolution);
                break;
            case R.id.pause:
                TvCameraSdk.getInstance().pauseRecord(cameraSurfaceView, outputPath);
                break;
            case R.id.stop:
                TvCameraSdk.getInstance().stopRecord(cameraSurfaceView);
                break;
            case R.id.resume:
                TvCameraSdk.getInstance().resumeRecord(cameraSurfaceView);
                break;
            case R.id.testMerge:
                Log.e("tvLog", String.valueOf(startTime - CameraSurfaceView.firstPreviewTime));
                //自定义视频拼接类
                String outputFile = Environment.getExternalStorageDirectory().getPath() + File.separator + "jack/c.mp4";
                String file1 = Environment.getExternalStorageDirectory().getPath() + File.separator + "jack/a.mp4";
                String file2 = Environment.getExternalStorageDirectory().getPath() + File.separator + "jack/b.mp4";
                List<String> mergeVideos = new ArrayList<>();
                mergeVideos.add(file1);
                mergeVideos.add(file2);
                try {
                    Mp4ParseUtil.appendMp4List(mergeVideos, outputFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.takePhoto:
                String takePhotofolder = Environment.getExternalStorageDirectory().getPath() + File.separator + "jack/photo/";
                String takePhotoName = System.currentTimeMillis() + ".jpg";
                String path = takePhotofolder + takePhotoName;
                TvCameraSdk.getInstance().takePhoto(getApplicationContext(), path);
                break;
            case R.id.delete:
                String deletePhotoFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "jack/photo/1514082470198.jpg";
                boolean flag = TvCameraSdk.getInstance().deleteVideoOrPhoto(getApplicationContext(), deletePhotoFilePath);

                String deleteVideoFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "cameraTest/1514018449089.mp4";
                boolean videoFlag = TvCameraSdk.getInstance().deleteVideoOrPhoto(getApplicationContext(), deleteVideoFilePath);
                Log.e(TAG, "删除照片=" + flag + ",删除视频=" + videoFlag);
                break;


        }
    }
}
